using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Services;
using MainGame.GamePlay;
using Microsoft.Xna.Framework.Input.Touch;
using Microsoft.Phone.Tasks;

namespace MainGame.Menu
{
    public interface IGames
    {
        event MenuEvent OnHBackClicked;
    }

    public class Games : BaseMenu, IGames
    {

        Button helpBackBtn, briscolaIcon, elevenIcon, crossroadsIcon, FratelliIcon, luxIcon, ElevenLiteIcon, TilesIcon; //, TutorialBtn;
        Vector2 savedPos;
        public bool Animated = false;
        public int Direction = 1, AnimVelocity = 60;
        SpriteBatch spriteBatch;
        Color textColor;
        SpriteFont smallFont;
        Vector2 textPos, startingDragPos;
        string helpText;
        private MarketplaceDetailTask detailTask;

        public Games(Game game, MenuDepths depth)
            : base(game, depth)
        {
            spriteBatch = Game.GetContext().SpriteBatch;
            background = game.Content.Load<Texture2D>(@"Images//Menu//HelpBg");
            bounds = new Rectangle(-game.GraphicsDevice.Viewport.Width, 0, game.GraphicsDevice.Viewport.Width, game.GraphicsDevice.Viewport.Height);
            game.Services.AddService(typeof(IGames), this);
            TouchPanel.EnabledGestures = GestureType.VerticalDrag | GestureType.DragComplete;
        }

        protected override void Dispose(bool disposing)
        {
            Game.Services.RemoveService(typeof(IGames));
            TouchPanel.EnabledGestures = GestureType.None;
            base.Dispose(disposing);
        }


        public override void Initialize()
        {
            savedPos.X = bounds.X;
            detailTask = new MarketplaceDetailTask();

            textPos = new Vector2(bounds.X, bounds.Y) + new Vector2(0, 30);
            helpText = Locale.Strings.HelpStr;
            startingDragPos = Vector2.Zero;
            // Load buttons
            helpBackBtn = new Button(Game, @"Images//Menu//button2", new Rectangle((int)bounds.X + 640, 370, 140, 55), Color.Blue, "small_font", Locale.Strings.Back, Color.White);
            AddButton(helpBackBtn, () =>
            {
                // Save the current pageGame
                Game.GetContext().currentConfig.currentPage = GamePage.main;
                SaveData.SaveUtils.SaveConfig(Game, Game.GetContext().currentConfig);

                helpBackBtn.touched = true;

                Animated = true;
                Direction = -1;
            });

            briscolaIcon = new Button(Game, @"Images//Menu//BriscolaIco", new Rectangle((int)bounds.X + 20, 90, 170, 170), Color.White, "small_font", "", Color.White);
            AddButton(briscolaIcon, () =>
            {
                detailTask.ContentIdentifier = "8b9ccc41-7c54-e011-854c-00237de2db9e";
                detailTask.Show();
            });

            elevenIcon = new Button(Game, @"Images//Menu//elevenIco", new Rectangle((int)bounds.X + 215, 90, 170, 170), Color.White, "small_font", "", Color.White);
            AddButton(elevenIcon, () =>
            {
                detailTask.ContentIdentifier = "5dbc6213-adf8-df11-9264-00237de2db9e";
                detailTask.Show();
            });

            crossroadsIcon = new Button(Game, @"Images//Menu//crossroadsIco", new Rectangle((int)bounds.X + 405, 90, 170, 170), Color.White, "small_font", "", Color.White);
            AddButton(crossroadsIcon, () =>
            {
                detailTask.ContentIdentifier = "4f2175bf-1dd2-df11-9eae-00237de2db9e";
                detailTask.Show();
            });

            FratelliIcon = new Button(Game, @"Images//Menu//FratelliIco", new Rectangle((int)bounds.X + 595, 90, 170, 170), Color.White, "small_font", "", Color.White);
            AddButton(FratelliIcon, () =>
            {
                detailTask.ContentIdentifier = "cda01759-1150-e011-854c-00237de2db9e";
                detailTask.Show();
            });

            luxIcon = new Button(Game, @"Images//Menu//luxIco", new Rectangle((int)bounds.X + 50, 285, 170, 170), Color.White, "small_font", "", Color.White);
            AddButton(luxIcon, () =>
            {
                detailTask.ContentIdentifier = "7e904d75-df2e-e011-854c-00237de2db9e";
                detailTask.Show();
            });

            ElevenLiteIcon = new Button(Game, @"Images//Menu//ElevenLiteIco", new Rectangle((int)bounds.X + 250, 285, 170, 170), Color.White, "small_font", "", Color.White);
            AddButton(ElevenLiteIcon, () =>
            {
                detailTask.ContentIdentifier = "2a8b0aad-aa4c-e011-854c-00237de2db9e";
                detailTask.Show();
            });

            TilesIcon = new Button(Game, @"Images//Menu//TilesIco", new Rectangle((int)bounds.X + 450, 285, 170, 170), Color.White, "small_font", "", Color.White);
            AddButton(TilesIcon, () =>
            {
                detailTask.ContentIdentifier = "173d8351-d351-e011-854c-00237de2db9e";
                detailTask.Show();
            });

            textColor = new Color(0, 0, 120);

            smallFont = Game.Content.Load<SpriteFont>("small_font");

            base.Initialize();
        }


        public override void Update(GameTime gameTime)
        {
            // Input: back button pressed
            if (((Game1)Game).IsBackButtonPressed())
            {
                //var ui_renderer = Game.Services.GetService(typeof(IUIRenderer)) as UIRenderer;
                //if (ui_renderer != null)
                //{
                //  foreach (ClickableButton cb in ui_renderer.buttons)
                //    if (cb.b.color == Color.Red)
                //      cb.b.color = Color.White;
                //}

                // Save the current pageGame
                Game.GetContext().currentConfig.currentPage = GamePage.main;
                SaveData.SaveUtils.SaveConfig(Game, Game.GetContext().currentConfig);

                Animated = true;
                Direction = -1;
            }

            if (Animated)
            {
                // Animate the menu items to follow the menu sliding animation
                bounds.X += Direction * AnimVelocity;
                textPos.X += Direction * AnimVelocity;

                foreach (Button b in (from cb in buttons select cb.b))
                    b.bounds.X += Direction * AnimVelocity;

                if (bounds.X > ((Game.GraphicsDevice.Viewport.Width - bounds.Width) / 2) && Direction == 1)
                {
                    Animated = false;
                    int offset = bounds.X;
                    bounds.X = ((Game.GraphicsDevice.Viewport.Width - bounds.Width) / 2);

                    foreach (Button b in (from cb in buttons select cb.b))
                        b.bounds.X += -Direction * offset;

                }

                if (bounds.X < savedPos.X)
                {
                    if (OnHBackClicked != null)
                        OnHBackClicked();
                }
            }

            //if (TouchPanel.IsGestureAvailable)
            //{
            //  var g = TouchPanel.ReadGesture();

            //  switch (g.GestureType)
            //  {
            //    case GestureType.DragComplete:
            //      startingDragPos = Vector2.Zero;
            //      break;
            //    case GestureType.VerticalDrag:
            //      if (g.Delta == Vector2.Zero)
            //      {
            //        startingDragPos = g.Position;
            //      }
            //      else
            //      {
            //        var dragQty = Vector2.Subtract(g.Position, startingDragPos);
            //        //if (Math.Abs(dragQty.Y) > 1f)

            //        textPos.Y += dragQty.Y;
            //        startingDragPos = g.Position;

            //        if (textPos.Y <= -300)
            //          textPos.Y = -300;
            //        else if (textPos.Y >= 300)
            //          textPos.Y = 300;

            //      }
            //      break;
            //    default:
            //      break;
            //  }
            //}

            base.Update(gameTime);
        }


        public override void Draw(GameTime gameTime)
        {
            base.Draw(gameTime);

            spriteBatch.Begin();
            spriteBatch.DrawString(Game.Content.Load<SpriteFont>(@"baveuse1_font"), Locale.Strings.TryOtherProducts, new Vector2((int)bounds.X + 125, 25), Color.Black, 0.0f, Vector2.Zero, 1.0f, SpriteEffects.None, 1.0f);
            spriteBatch.DrawString(Game.Content.Load<SpriteFont>(@"baveuse1_font"), Locale.Strings.TryOtherProducts, new Vector2((int)bounds.X + 120, 20), Color.Red, 0.0f, Vector2.Zero, 1.0f, SpriteEffects.None, 1.0f);

            //spriteBatch.Draw(Game.Content.Load<Texture2D>(@"white_pixel"), new Rectangle((int)bounds.X + 115, 125, 100, 100), new Color(255, 255, 255, 200));
            //spriteBatch.Draw(Game.Content.Load<Texture2D>(@"Images/Menu/Sun"), new Rectangle((int)bounds.X + scopaIcon.bounds.X - 20, scopaIcon.bounds.Y - 20, 140, 140), new Color(255, 255, 255, 200));      
            //spriteBatch.DrawString(smallFont, helpText.Replace("\\n", "\n"), textPos + Vector2.One, Color.Black, 0.0f, Vector2.Zero, 0.8f, SpriteEffects.None, 0f);
            //spriteBatch.DrawString(smallFont, helpText.Replace("\\n", "\n"), textPos, Color.White, 0.0f, Vector2.Zero, 0.8f, SpriteEffects.None, 0f);

            spriteBatch.End();


        }

        public event MenuEvent OnHBackClicked;
    }
}
